package me.xADudex.RandomLocation;

import org.bukkit.plugin.*;
import org.bukkit.entity.*;
import java.util.*;

public class Timer
{
    private Plugin pl;
    private int processID;
    private boolean running;
    private HashMap<String, Integer> gods;
    private HashMap<String, Integer> signs;
    private HashMap<String, Integer> portals;
    private HashMap<String, Integer> cmds;
    private HashMap<String, Integer> portalWalkers;
    
    public Timer(final Plugin rl) {
        this.running = false;
        this.gods = new HashMap<String, Integer>();
        this.signs = new HashMap<String, Integer>();
        this.portals = new HashMap<String, Integer>();
        this.cmds = new HashMap<String, Integer>();
        this.portalWalkers = new HashMap<String, Integer>();
        this.pl = rl;
    }
    
    public void addGod(final Player p, final int s) {
        this.gods.put(p.getName(), s);
    }
    
    public void addSigns(final Player p, final int s) {
        this.signs.put(p.getName(), s);
    }
    
    public void addPortals(final Player p, final int s) {
        this.portals.put(p.getName(), s);
    }
    
    public void addcmds(final Player p, final int s) {
        this.cmds.put(p.getName(), s);
    }
    
    public void addPortalWalkers(final Player p) {
        this.portalWalkers.put(p.getName(), 3);
    }
    
    public boolean godsContains(final Player p) {
        return this.gods.containsKey(p.getName());
    }
    
    public boolean signsContains(final Player p) {
        return this.signs.containsKey(p.getName());
    }
    
    public boolean portalsContains(final Player p) {
        return this.portals.containsKey(p.getName());
    }
    
    public boolean cmdsContains(final Player p) {
        return this.cmds.containsKey(p.getName());
    }
    
    public boolean portalWalkersContains(final Player p) {
        return this.portalWalkers.containsKey(p.getName());
    }
    
    public int getGodTimeLeft(final Player p) {
        if (this.gods.containsKey(p.getName())) {
            return this.gods.get(p.getName());
        }
        return 0;
    }
    
    public int getCmdTimeLeft(final Player p) {
        if (this.cmds.containsKey(p.getName())) {
            return this.cmds.get(p.getName());
        }
        return 0;
    }
    
    public int getPortalTimeLeft(final Player p) {
        if (this.portals.containsKey(p.getName())) {
            return this.portals.get(p.getName());
        }
        return 0;
    }
    
    public int getSignTimeLeft(final Player p) {
        if (this.signs.containsKey(p.getName())) {
            return this.signs.get(p.getName());
        }
        return 0;
    }
    
    public int getPortalWalkersTimeLeft(final Player p) {
        if (this.portalWalkers.containsKey(p.getName())) {
            return this.portalWalkers.get(p.getName());
        }
        return 0;
    }
    
    public void startTimer() {
        if (!this.running) {
            this.running = true;
            this.processID = this.pl.getServer().getScheduler().scheduleSyncRepeatingTask(this.pl, (Runnable)new Runnable() {
                @Override
                public void run() {
                    final ArrayList<String> al = new ArrayList<String>();
                    al.addAll(Timer.this.gods.keySet());
                    for (final String p : al) {
                        if (Timer.this.gods.get(p) <= 1) {
                            Timer.this.gods.remove(p);
                        }
                        else {
                            Timer.this.gods.put(p, Timer.this.gods.get(p) - 1);
                        }
                    }
                    al.clear();
                    al.addAll(Timer.this.signs.keySet());
                    for (final String p : al) {
                        if (Timer.this.signs.get(p) <= 1) {
                            Timer.this.signs.remove(p);
                        }
                        else {
                            Timer.this.signs.put(p, Timer.this.signs.get(p) - 1);
                        }
                    }
                    al.clear();
                    al.addAll(Timer.this.portals.keySet());
                    for (final String p : al) {
                        if (Timer.this.portals.get(p) <= 1) {
                            Timer.this.portals.remove(p);
                        }
                        else {
                            Timer.this.portals.put(p, Timer.this.portals.get(p) - 1);
                        }
                    }
                    al.clear();
                    al.addAll(Timer.this.cmds.keySet());
                    for (final String p : al) {
                        if (Timer.this.cmds.get(p) <= 1) {
                            Timer.this.cmds.remove(p);
                        }
                        else {
                            Timer.this.cmds.put(p, Timer.this.cmds.get(p) - 1);
                        }
                    }
                    al.clear();
                    al.addAll(Timer.this.portalWalkers.keySet());
                    for (final String p : al) {
                        if (Timer.this.portalWalkers.get(p) <= 1) {
                            Timer.this.portalWalkers.remove(p);
                        }
                        else {
                            Timer.this.portalWalkers.put(p, Timer.this.portalWalkers.get(p) - 1);
                        }
                    }
                    al.clear();
                }
            }, 0L, 20L);
        }
    }
    
    public void stopTimer() {
        if (this.running) {
            this.pl.getServer().getScheduler().cancelTask(this.processID);
        }
    }
}
